package com.hanbang.maige.view.huodong.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hanbang.maige.R;
import com.hanbang.maige.code.MyApplication;
import com.hanbang.maige.code.base.baseadapter.ViewHolder;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.view.activity.BaseSwipeActivity;
import com.hanbang.maige.mode.enumeration.HuoDongStatus;
import com.hanbang.maige.mode.javabean.huodong.BaomingPeoplesData;
import com.hanbang.maige.mode.javabean.huodong.HuoDongDetailData;
import com.hanbang.maige.pressenter.huodong.HuoDongDetailPresenter;
import com.hanbang.maige.utils.bitmap.GlideOptions;
import com.hanbang.maige.utils.bitmap.GlideUtils;
import com.hanbang.maige.utils.other.DateUtils;
import com.hanbang.maige.utils.other.StringUtils;
import com.hanbang.maige.utils.ui.ToastUtils;
import com.hanbang.maige.view.huodong.IHuoDongBaseView;
import com.hanbang.maige.view.huodong.adapter.HuoDongAdapter;
import com.hanbang.maige.widget.ScaleImageView;
import com.hanbang.maige.widget.button.FlatButton;
import com.hanbang.maige.widget.countdown.CountdownView;
import com.hanbang.maige.widget.empty_layout.ContextData;
import com.hanbang.maige.widget.gridlayout.SuperGridLayout;

import butterknife.BindView;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by 方朝辉 on 2016/10/26.
 */

public class HuoDongDetailActivity extends BaseSwipeActivity<IHuoDongBaseView.IHuoDongDetailView, HuoDongDetailPresenter>
        implements IHuoDongBaseView.IHuoDongDetailView, HuoDongAdapter.OnButtnClickListener {
    @BindView(R.id.adIm)
    ScaleImageView adIm;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.addrTv)
    TextView addrTv;
    @BindView(R.id.jieshaoTv)
    TextView jieshaoTv;
    @BindView(R.id.baomingNum)
    TextView baomingNum;
    @BindView(R.id.timeTv)
    TextView timeTv;
    @BindView(R.id.countDownView)
    CountdownView countDownView;
    @BindView(R.id.renshuGridLayout)
    SuperGridLayout renshuGridLayout;
    @BindView(R.id.submitBm)
    FlatButton submitBm;

    HuoDongDetailData huoDongDetailData;
    private String huodongId;

    private CommonAdapter<BaomingPeoplesData> adapter = new CommonAdapter<BaomingPeoplesData>(this, R.layout.item_image, null) {
        @Override
        public void convert(ViewHolder holder, BaomingPeoplesData baomingPeoplesData) {
            SuperGridLayout.LayoutParams params = renshuGridLayout.generateDefaultLayoutParams();
            params.height = renshuGridLayout.getmItemSize();
            params.width = renshuGridLayout.getmItemSize();
            holder.getConvertView().setLayoutParams(params);
            holder.setImageBitmap(R.id.image, baomingPeoplesData.getHeadPhotoSma(),
                    new GlideOptions.Builder().setPlaceholder1_1()
                            .setWidth(renshuGridLayout.getmItemSize())
                            .setHeight(renshuGridLayout.getmItemSize())
                            .addTransformation(new CropCircleTransformation(MyApplication.myApp)));
        }
    };

    @Override
    public HuoDongDetailPresenter initPressenter() {
        return new HuoDongDetailPresenter();
    }


    public static void startUI(Context context, String huodongId) {
        if (StringUtils.isBlank(huodongId)) {
            ToastUtils.showShort(context, "活动不存在");
            return;
        }
        Intent intent = new Intent(context, HuoDongDetailActivity.class);
        intent.putExtra("huodongId", huodongId);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_huodong_detail;
    }

    @Override
    public void initView() {
        super.initView();
        toolbar.setBack(this);
        renshuGridLayout.setAdapter(adapter);

    }

    @Override
    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return swipe;
    }

    @Override
    public void baomingSuccess(String msg, int postion) {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content(msg).title("提示")
                .positiveText("知道了")
                .build();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                swipe.setRefreshing(true);
            }
        });
        dialog.show();
    }

    @Override
    public void onBaoming(int position, String activityId) {
        presenter.postBaoming(activityId, position);
    }

    @Override
    public void updataUI(HuoDongDetailData result) {
        if (result != null) {
            huoDongDetailData = result;
            toolbar.setSuperTitle(result.getActivityName());

            nameTv.setText(result.getNiChenName());
            addrTv.setText(result.getActivityAddress());
//            timeTv.setText();
            jieshaoTv.setText(result.getActiveJieShao());
            GlideUtils.show(adIm, result.getActiveHaiBaoMid(), new GlideOptions.Builder()
                    .setPlaceholder(R.mipmap.default_9_16)
                    .setError(R.mipmap.default_9_16).bulider());
            baomingNum.setText(result.getCanyuPeopleCount() + " 人");

            countDownView.setEndShowData(HuoDongStatus.STOP.getValuse());
            HuoDongStatus status = result.getHuoDongStatus();
            submitBm.setText(status.getHint());
            submitBm.setEnabled(status == HuoDongStatus.ACTIONING);
            if (status == HuoDongStatus.NO_START) {
                countDownView.setDataStr(DateUtils.dateToString(result.getStartTime(), "MM-dd HH:mm"));
            } else if (status == HuoDongStatus.ACTIONING) {
                countDownView.setDataStr(DateUtils.dateToString(result.getEndTime(), "MM-dd HH:mm"));
            } else if (status == HuoDongStatus.STOP) {
                countDownView.setDataStr(status.getValuse());
            }
            countDownView.start(result.getActivedjs());
            timeTv.setText(result.getTime());
            adapter.setmDatas(result.getCanyuPeoples());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void parseIntent(Intent intent) {
        huodongId = intent.getStringExtra("huodongId");
    }

    @Override
    public void clearData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter.getHttpData(huodongId);
    }

    @OnClick(R.id.submitBm)
    public void onClick() {
        if (huoDongDetailData != null) {
            presenter.postBaoming(huodongId, 0);
        }
    }


    @Override
    public void onRefresh() {
        presenter.getHttpData(huodongId);
    }

    @Override
    public void onRetryClick(ContextData data) {
        presenter.getHttpData(huodongId);
    }

    @Override
    public void onEmptyClick(ContextData data) {
        presenter.getHttpData(huodongId);
    }
}
